package com.l.d.app.library.eventbus;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;

import org.simple.eventbus.EventBus;

/**
 * Created by 林德 on 2017/4/5 11:00.
 * <p/>
 */
public class EventSend
{
    @NonNull
    private final String tag;
    @Nullable
    private Object uniqueIdentifier;
    @Nullable
    private Object msg;

    private EventSend(@NonNull String tag)
    {
        if (TextUtils.isEmpty(tag))
        {
            throw new NullPointerException("tag can not be null or\"\"!");
        }
        this.tag = tag;
    }

    public static void register(@NonNull Object subscriber)
    {
        EventBus.getDefault().register(subscriber);
    }

    public static void unregister(@NonNull Object subscriber)
    {
        EventBus.getDefault().unregister(subscriber);
    }

    public static EventSend with(@NonNull String tag)
    {
        return new EventSend(tag);
    }

    public EventSend uniqueIdentifier(Object uniqueIdentifier)
    {
        this.uniqueIdentifier = uniqueIdentifier;
        return this;
    }

    public EventSend msg(@Nullable Object msg)
    {
        this.msg = msg;
        return this;
    }

    public void build()
    {
        EventSendEntity entity = new EventSendEntity(uniqueIdentifier, msg);
        EventBus.getDefault().post(entity, tag);
    }
}
